﻿@{ ViewData["Title"] = "Index";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml"; }
@section head{
    @*<style type="text/css">
            .layui-table-cell {
                height: 45px;
                line-height: 45px;
            }
        </style>*@
    <script src="~/js/CommonHelper.js"></script>
    <script type="text/javascript">
        var table;
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'form'], function () {
            var $ = layui.$
                , form = layui.form
                , table = layui.table;

            table.render({
                elem: '#table-datalist'
                , url: '/Console/SystemConfigTab/GetListJson'
                , limit: 10
                , title: '基础配置表'
                , where: {}
                , cols: [[{ field: 'title', title: '系统设置' }
                //    , {
                //    field: 'status', title: '分类状态', templet: function (res) {
                //        if (res.status)
                //            return '<input type="checkbox" name="status" lay-skin="switch" lay-text="显示|隐藏" lay-filter="table-state" value="' + res.id + '" checked="checked">'
                //        else
                //            return '<input type="checkbox" name="status" lay-skin="switch" lay-text="显示|隐藏" lay-filter="table-state" value="' + res.id + '">'
                //    }
                //}
                    , { fixed: 'right', title: '操作', width: 100, toolbar: '#table-datalist-bar' }
                ]]
                , page: { layout: ['prev', 'page', 'next', 'refresh'] }
                , parseData: function (res) {
                }
            });
            //监听新增操作
            form.on('submit(addNew)', function (data) {
                var type = $("#Type").val();
                var source = $("#Source").val();
                var index = layer.open({
                    type: 2,
                    title: '新增数据',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['800px', '600px'],
                    content: '/Console/SystemConfigTab/Add'
                });
                layer.full(index);
            });
            //监听批量删除
            form.on('submit(DelItems)', function (data) {
                var checkStatus = table.checkStatus('table-datalist');
                var data = checkStatus.data;
                if (data.length > 0) {
                    var ids = "";
                    for (var i = 0; i < data.length; i++) {
                        if (ids == "")
                            ids = data[i].id;
                        else
                            ids += "," + data[i].id;
                    }
                    if (ids != "") {
                        layer.confirm('确定要删除选中的数据吗？', function (index) {
                            $.ajax({
                                type: "POST",
                                url: "/Console/SystemConfigTab/DeleteItems",
                                data: { ids: ids }, // 你的formid
                                async: true,
                                error: function (request) {
                                    alert("Connection error");
                                },
                                beforeSend: function (xhr) {
                                    layui.layer.load();
                                },
                                success: function (data) {
                                    var item = eval(data);
                                    if (item.code == 0) {
                                        layer.msg("删除成功", { icon: 1, time: 1000 }, function () {
                                            table.reload("table-datalist");
                                        });
                                    } else if (item.code == 1) {
                                        layer.msg(item.msg, { icon: 2 });
                                    } else {
                                        layer.msg("删除失败", { icon: 2 });
                                    }
                                    layer.closeAll('loading');
                                }
                            });
                        });
                    } else {
                        layer.msg("请选择需要删除的数据", { icon: 0 });
                    }
                } else {
                    layer.msg("请选择需要删除的数据", { icon: 0 });
                }
            });
            //监听行工具事件
            table.on('tool(table-datalist)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('确定要删除此数据吗？', function (index) {
                        $.ajax({
                            type: "POST",
                            url: "/Console/SystemConfigTab/Delete",
                            data: { id: data.id }, // 你的formid
                            async: true,
                            error: function (request) {
                                alert("Connection error");
                            },
                            beforeSend: function (xhr) {
                                layui.layer.load();
                            },
                            success: function (data) {
                                var item = eval(data);
                                if (item.code == 0) {
                                    layer.msg("删除成功", { icon: 1, time: 1000 }, function () {
                                        table.reload("table-datalist");
                                    });
                                } else if (item.code == 1) {
                                    layer.msg(item.msg, { icon: 2 });
                                } else {
                                    layer.msg("删除失败", { icon: 0 });
                                }
                                layer.closeAll('loading');
                            }
                        });
                    });
                } else if (obj.event === 'edit') {
                    var index = layer.open({
                        type: 2,
                        title: '编辑数据',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['800px', '600px'],
                        content: '/Console/SystemConfigTab/Edit?id=' + data.id
                    });
                    layer.full(index);
                } else if (obj.event == 'needpage') {
                    if (Number(data.id) == 13)
                        window.location.href = "/Console/SystemConfig/CustomerService";
                    else
                        window.location.href = "/Console/SystemConfig/SetPage?pid=" + data.id;
                }
            });
            var typeval = 0, sourceval = 0;
            //监听锁定操作
            form.on('switch(table-state)', function (obj) {
                var state = 0;
                if (obj.elem.checked) {
                    state = 1;
                } else {
                    state = 0;
                }

                $.ajax({
                    type: "POST",
                    url: "/Console/SystemConfigTab/UpdateState",
                    data: { id: this.value, intState: state }, // 你的formid
                    async: true,
                    error: function (request) {
                        alert("Connection error");
                    },
                    beforeSend: function (xhr) {
                        layui.layer.load();
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            layer.msg("操作成功", { icon: 1, time: 1000 }, function () {
                                table.reload("table-datalist");
                            });
                        } else if (item.code == 1) {
                            layer.msg(item.msg, { icon: 2 });
                        } else {
                            layer.msg("保存失败", { icon: 0 });
                        }
                        layer.closeAll('loading');
                    }
                });
            });
            /* 监听提交 */
            form.on('submit(search)', function (data) {
                var params = {};
                params["title"] = $("#_title").val();
                params["status"] = $("select[name='_status']").val();
                table.reload('table-datalist', {
                    where: params,
                    page: { curr: 1, layout: ['prev', 'page', 'next', 'refresh'] }
                });
                return false;
            });
        });
    </script>
}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                @*<div class="layui-card-header">基础配置表</div>*@
                <div class="layui-card-body">
                    <table class="layui-hide" id="table-datalist" lay-filter="table-datalist"></table>
                    <script type="text/html" id="table-datalist-bar">
                        @*<button class="layui-btn layui-btn-sm" lay-event="edit">编辑</button>*@
                        <button class="layui-btn layui-btn-sm" lay-event="needpage">下级管理</button>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>